package com.woniuxy.search.service.impl;

import com.woniuxy.common.model.Pages;
import com.woniuxy.search.bean.BorrowRecordIndexVo;
import com.woniuxy.search.bean.BorrowRecordSearchQuery;
import com.woniuxy.search.service.ISearchService;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yellow Doctor
 * @date 2025-04-03
 * @desc 蜗牛创想：www.woniuxy.com
 */
@Service
public class ISearchServiceImpl implements ISearchService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     *   QueryBuilders.matchQuery("name","指定字段查询")
     *       name 中查询  张三
     *   QueryBuilders.queryStringQuery("多个字段查询")
     *       ['name','desc'] 中查询 张三
     *   QueryBuilders.QueryBuilders.matchAllQuery()
     *       查询所有 不需要参数
     *   QueryBuilders.rangeQuery("period")  范围查询
     *
     * @param query
     * @return
     */
    @Override
    public Pages<BorrowRecordIndexVo> search(BorrowRecordSearchQuery query) {


        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //01 构造 queryBuild
        String name = query.getName();
        if (!StringUtils.isEmpty(name)){
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name", query.getName());
            builder.withQuery(queryBuilder);
        }
        if (query.getEndPeriod() != null && query.getEndPeriod() != 0){
            RangeQueryBuilder period = QueryBuilders.rangeQuery("period")
                    .gte(query.getStartPeriod())
                    .lte(query.getEndPeriod());

            builder.withQuery(period);
        }
        //分页
        builder.withPageable(PageRequest.of(query.getPageNo() - 1, query.getPageSize()));
        builder.withSort(SortBuilders.fieldSort("createdate").order(SortOrder.DESC));
        //02 搜索
        SearchHits<BorrowRecordIndexVo> searchHits =
                elasticsearchRestTemplate.search(builder.build(), BorrowRecordIndexVo.class);
        Long totalHits = searchHits.getTotalHits();
        List<SearchHit<BorrowRecordIndexVo>> hitList = searchHits.getSearchHits();
        //03 查询的  结果转成 java对象
        List<BorrowRecordIndexVo> list =
                hitList.stream().map(SearchHit::getContent).collect(Collectors.toList());

        // 返回
        Pages<BorrowRecordIndexVo> pages = new Pages<>();
        pages.setList(list);
        pages.setTotal(totalHits);
        pages.setPageNum(query.getPageNo());
        pages.setPageSize(query.getPageSize());

        return pages;
    }
}
